import requests
import json
import time


def amap_weather():
    reporttime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    url = "https://restapi.amap.com/v3/weather/weatherInfo"
    key = '高德API'
    data = {'key': key, "city": 城市代码}
    ajson = requests.post(url, data)
    info = dict(ajson.json())
    info = dict(info)
    newinfo = info['lives'][0]
    #reporttime = newinfo['reporttime']  # 当前日期和时间
    city = newinfo['province'] + newinfo['city']  # 城市
    weather = newinfo['weather']  # 天气
    temperature = newinfo['temperature'] + '℃'  # 温度
    winddirection = newinfo['winddirection']  # 风向
    windpower = newinfo['windpower']+"级"  # 风力
    humidity = newinfo['humidity']+"%"  # 湿度
    aggregate = ("现在是:" + reporttime + "\n当前城市:" + city + "\n今天天气:" + weather + "\n温度:" +
                 temperature + "\n风向:" + winddirection + "\n风力:" + windpower + "\n湿度:" + humidity)
    headers = {"Content-Type": "text/plain"}
    content = aggregate
    data = {
        "msgtype": "text",
        "text": {
            "content": content,
        }
    }
    output = requests.post(
        url='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=企业微信机器人API',
        headers=headers, json=data)
    print(output.text)
amap_weather()
